package com.fengye.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengye.api.service.ArticleService;
import com.fengye.common.constants.CommonConstant;
import com.fengye.common.utils.BeanCopyUtils;
import com.fengye.common.domain.entity.Article;
import com.fengye.common.domain.entity.Category;
import com.fengye.common.domain.vo.CategoryVo;
import com.fengye.api.mapper.CategoryMapper;
import com.fengye.api.service.CategoryService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author sunlei
* @description 针对表【fy_category】的数据库操作Service实现
* @date 2024-02-25 15:52:07
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Override
    public List<CategoryVo> getCategoryList() {
        // 查询文章表 状态为已发布的文章
        LambdaQueryWrapper<Article> articleQueryWrapper = new LambdaQueryWrapper<>();
        articleQueryWrapper.eq(Article::getStatus, CommonConstant.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articleQueryWrapper);
        //获取文章的分类id，并去重
        List<Long> categoryIdList = articleList.stream().map(Article::getCategoryId).distinct().collect(Collectors.toList());
        // 查询分类列表
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        List<Category> categoryList = listByIds(categoryIdList);

        return BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
    }
}




